﻿using System;
using System.Collections.Generic;

using Apache.Cassandra;

namespace RexToy.Cassandra.Meta
{
    internal class ColumnFamilyInfo
    {
        private CfDef _def;
        private Dictionary<object, ColumnInfo> _colInfos;
        internal ColumnFamilyInfo(CfDef def)
        {
            def.ThrowIfNullArgument("def");
            _def = def;
            _colInfos = new Dictionary<object, ColumnInfo>();
            foreach (var colDef in def.Column_metadata)
            {
                var colInfo = new ColumnInfo(colDef, this);
                _colInfos.Add(colInfo.Name, colInfo);
            }
        }

        public double BloomFilterFpChance
        {
            get { return _def.Bloom_filter_fp_chance; }
        }

        public string Caching
        {
            get { return _def.Caching; }
        }

        public ColumnType ColumnType
        {
            get 
            {
                return EnumEx.Parse<ColumnType>(_def.Column_type);
            }
        }

        public string Comment
        {
            get { return _def.Comment; }
        }

        public string CompactionStrategy
        {
            get { return _def.Compaction_strategy; }
        }

        public IReadOnlyDictionary<string, string> CompactionStrategyOptions
        {
            get { return _def.Compaction_strategy_options; }
        }

        public string ComparatorType
        {
            get { return _def.Comparator_type; }
        }

        public IMarshal Comparator
        {
            get
            {
                return MarshalFactory.CreateMarshal(_def.Comparator_type);
            }
        }

        public IReadOnlyDictionary<string, string> CompressionOptions
        {
            get { return _def.Compression_options; }
        }

        public double DclocalReadRepairChance
        {
            get { return _def.Dclocal_read_repair_chance; }
        }

        public string DefaultValidationClass
        {
            get { return _def.Default_validation_class; }
        }

        public IMarshal DefaultValidation
        {
            get
            {
                return MarshalFactory.CreateMarshal(_def.Default_validation_class);
            }
        }

        public int GcGraceSeconds
        {
            get { return _def.Gc_grace_seconds; }
        }

        public int Id
        {
            get { return _def.Id; }
        }

        //TODO:Check type
        //public string KeyAlias
        //{
        //    get { return _def.Key_alias == null ? string.Empty : _u.GetString(_def.Key_alias); }
        //}

        public int KeyCacheSavePeriodInSeconds
        {
            get { return _def.Key_cache_save_period_in_seconds; }
        }

        public double KeyCacheSize
        {
            get { return _def.Key_cache_size; }
        }

        public string KeyValidationClass
        {
            get { return _def.Key_validation_class; }
        }

        public IMarshal KeyValidation
        {
            get
            {
                return MarshalFactory.CreateMarshal(_def.Key_validation_class);
            }
        }

        public string KeySpace
        {
            get { return _def.Keyspace; }
        }

        public int MaxCompactionThreshold
        {
            get { return _def.Max_compaction_threshold; }
        }

        public int MemtableFlushAfterMins
        {
            get { return _def.Memtable_flush_after_mins; }
        }

        public double MemtableOperationsInMillions
        {
            get { return _def.Memtable_operations_in_millions; }
        }

        public int MemtableThroughputInMb
        {
            get { return _def.Memtable_throughput_in_mb; }
        }

        public double MergeShardsChance
        {
            get { return _def.Merge_shards_chance; }
        }

        public int MinCompactionThreshold
        {
            get { return _def.Min_compaction_threshold; }
        }

        public string Name
        {
            get { return _def.Name; }
        }

        public double ReadRepairChance
        {
            get { return _def.Read_repair_chance; }
        }

        public bool ReplicateOnWrite
        {
            get { return _def.Replicate_on_write; }
        }

        public int RowCacheKeysToSave
        {
            get { return _def.Row_cache_keys_to_save; }
        }

        public string RowCacheProvider
        {
            get { return _def.Row_cache_provider; }
        }

        public int RowCacheSavePeriodInSeconds
        {
            get { return _def.Row_cache_save_period_in_seconds; }
        }

        public double RowCacheSize
        {
            get { return _def.Row_cache_size; }
        }

        public string SubComparatorType
        {
            get { return _def.Subcomparator_type; }
        }

        public IMarshal SubComparator
        {
            get
            {
                return MarshalFactory.CreateMarshal(_def.Subcomparator_type);
            }
        }

        public ColumnInfo GetColumnInfo(object col)
        {
            ColumnInfo info;
            bool got = _colInfos.TryGetValue(col, out info);
            if (got)
                return info;
            else
                return null;
        }
    }
}